package net.gz01.wallet.web.controller.org;



import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;

import net.gz01.util.ZODate;
import net.gz01.util.ZOMD5;
import net.gz01.wallet.common.ZOSearchCondition;
import net.gz01.wallet.web.controller.BaseController;
import net.gz01.wallet.web.model.Org;
import net.gz01.wallet.web.model.OrgRole;
import net.gz01.wallet.web.model.OrgUser;

public class OrgUserController extends BaseController{

	public void index(){
		setAttr("sidebar", getSession().getAttribute("sidebar"));
		render("index.html");
	}

	public void search(){
		ZOSearchCondition sc = super.getSearchCondition();
		OrgUser orgUser = (OrgUser) super.getSession().getAttribute("loginOrgUser");
		Page<OrgUser> items = OrgUser.dao.search(sc,orgUser.getOrgCode());
		super.setPageResultToRequest("items", items);
		render("system/orgUserList.html");
	}

	public void addPage(){
		OrgUser orgUser = (OrgUser) super.getSession().getAttribute("loginOrgUser");
		setAttr("org", Org.dao.selectByOrgCode(orgUser.getOrgCode()));
		setAttr("roleList", OrgRole.dao.selectAllByOrgCode(orgUser.getOrgCode()));
		render("system/orgUserAdd.html");
	}

	public void updatePage(){
		OrgUser orgUser = (OrgUser) super.getSession().getAttribute("loginOrgUser");
		OrgUser user = OrgUser.dao.findById(getPara("id"));
		setAttr("org", Org.dao.selectByOrgCode(orgUser.getOrgCode()));
		setAttr("roleList", OrgRole.dao.selectAllByOrgCode(orgUser.getOrgCode()));
		setAttr("orgUser", user);
		render("system/orgUserUpdate.html");
	}

	public void add(){
		OrgUser orgUser = (OrgUser) super.getSession().getAttribute("loginOrgUser");
		OrgUser user = getModel(OrgUser.class);
		if(OrgUser.dao.selectByUserName(user.getUserName()) != null){
			renderFail("用户名已被使用.");
		}else{
			orgUser.setUpdateTime(ZODate.getDateTime());
			orgUser.setPassword(ZOMD5.codeFor(orgUser.getPassword()));
			orgUser.setOrgCode(orgUser.getOrgCode());
			orgUser.setEnable(true);
			orgUser.setIsAdmin(false);
			orgUser.save();
			renderSuccess();
		}
	}

	public void update(){
		OrgUser orgUser = getModel(OrgUser.class,true);
		String newPassword = getPara("newPassword");
		if(StrKit.notBlank(newPassword)){
			orgUser.setPassword(ZOMD5.codeFor(newPassword));
		}
		orgUser.setUpdateTime(ZODate.getDateTime());
		orgUser.update();
		renderSuccess();
	}

	public void delete(){
		OrgUser.dao.deleteById(getPara("id"));
		renderSuccess(false);
	}

	public void changeEnable(){
		OrgUser.dao.changeEnable(getParaToInt("id"));
		renderSuccess(false);
	}

	public void changePasswordPage(){
		render("changePassword.html");
	}

	public void changePassword(){
		String userId = getPara("userId");
		String oldPassword = getPara("oldPassword");
		String newPassword = getPara("newPassword");
		OrgUser user = OrgUser.dao.findById(userId);
		if(user.getPassword().equalsIgnoreCase(ZOMD5.codeFor(oldPassword))){
			user.setPassword(ZOMD5.codeFor(newPassword));
			user.update();
			renderSuccess("修改密码成功，下次登录生效！");
		}else{
			renderFail("旧密码错误.");
		}
	}

}
